﻿
using EMSClient.Config;
using Oliving.Common;
using Oliving.Common.MyHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace MeetingClient
{
    public partial class FrmRegist : Form
    {

        #region 【构造方法】
        public FrmRegist()
        {
            InitializeComponent();
        }

        private string _type = "";

        public FrmRegist(string type)
        {
            InitializeComponent();
            lbl_ExpireTime.Visible = false;
            if (type == "Main")
            {
                _type = "Main";

                txtMac.Text = Register.ReadMachineCode();
                string code = EmsConfigHelper.Regist;
                txtCode.Text = code;
                if (code.Length > 0)
                {
                    DateTime dt = new DateTime();
                    try
                    {
                        if (Register.VerifyRegisterCode(txtMac.Text, code, out dt))
                        {
                            string strT = dt.ToString("yyyy-MM-dd");
                            lbl_ExpireTime.Visible = true;
                            this.lbl_ExpireTime.Text = "已注册，软件注册的截至日期为:" + strT;
                        }
                    }
                    catch
                    {
                        MessageBox.Show("注册失败！", "注册信息：");
                    }
                }
            }
        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            if (lbl_ExpireTime.Visible)
            {
                this.Close();
                this.DialogResult = DialogResult.OK;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //            ConfigManager.UpdateAppSettingsItemValue(ConfigManager.config, "Regist", txtCode.Text.Trim());


            //          ConfigHelper.SetValue("Regist", txtCode.Text.Trim());
            EmsConfigHelper.Regist = txtCode.Text.Trim();
            ////////////////////////////////////////

            if (txtCode.Text.Trim().Equals(""))
            {
                MessageBox.Show("请输入注册码！", "提醒");
            }
            else
            {
                try
                {
                    DateTime dt = new DateTime();
                    if (Register.VerifyRegisterCode(txtMac.Text, txtCode.Text.Trim(), out dt))
                    {
                        if (dt > DateTime.Now)
                        {
                            EmsConfigHelper.Regist = txtCode.Text.Trim();
                           // ConfigHelper.SetValue("Regist", txtCode.Text.Trim());

                            string strT = dt.ToString("yyyy-MM-dd");
                            lbl_ExpireTime.Text = "注册成功！    注册截至到：" + strT;
                            lbl_ExpireTime.Visible = true;
                            MessageBox.Show("注册成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                        else
                        {
                            MessageBox.Show("您输入的注册码已过期！", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
                    else
                    {
                        MessageBox.Show("您输入的注册码有误！", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch
                {
                    MessageBox.Show("您输入的注册码有误！", "提醒", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }




    }
}
